///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package com.pclong.aop;
//
//import java.lang.reflect.Method;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import org.simpleframework.xml.Order;
//import org.springframework.aop.AfterReturningAdvice;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.mail.MailException;
//import org.springframework.mail.MailSender;
//import org.springframework.mail.SimpleMailMessage;
//
///**
// *
// * @author Administrator
// */
//public class SendOrderConfirmationEmailAdvice implements AfterReturningAdvice, InitializingBean {
//    // user jes on localhost
//    private static final String DEFAULT_MAIL_FROM = "test@pprun.org";
//    
//    private static final String DEFAULT_SUBJECT = "Thank you for your order!";
//    
//    private final Log logger = LogFactory.getLog(getClass());
//    
//    private MailSender mailSender;
//    
//    private String mailFrom = DEFAULT_MAIL_FROM;
//    
//    private String subject = DEFAULT_SUBJECT;
//    
//    public void setMailSender(MailSender mailSender) {
//        this.mailSender = mailSender;
//    }
//    
//    public void setMailFrom(String mailFrom) {
//        this.mailFrom = mailFrom;
//    }
//    
//    public void setSubject(String subject) {
//        this.subject = subject;
//    }
//    
//    public void throws Exception {
//        if (this.mailSender == null) {
//            throw new IllegalStateException("mailSender is required");
//        }
//    }
//    
//    /**
//     * 
//     * @param returnValue 被拦截的方法的返回值
//     * @param m 被拦截的方法的所有信息（Method类封装了这些信息）
//     * @param args 被拦截的方法的所有参数组成的数组
//     * @param target 目标对象，对于方法执行来说，即是方法所在的类的实例（与 this 同，批当前对象）
//     * @throws java.lang.Throwable 
//     */
//    public void afterReturning(Object returnValue, Method m, Object[] args, Object target) throws Throwable {
//        // 我们被拦截的方法为 void insertOrder(Order order)，方法只有一个参数，所以可知数据的第1个元素即是被传进的　order 对象
//        // 得到了order 对象，就可以将 order 对应的帐户名及帐单号发送到邮件中，以便确认无误。
//        Order order = (Order) args[0];
//        Account account = ((PetStoreFacade) target).getAccount(order.getUser().getUsername());
//        
//        // don't do anything if email address is not set
//        if (account.getEmail() == null || account.getEmail().length() == 0) {
//            return;
//        }
//        
//        StringBuffer text = new StringBuffer();
//        text.append("Dear ").append(account.getFirstname()).
//                append(' ').append(account.getLastname());
//        text.append(", thank your for your order from JPetStore. " +
//                "Please note that your order number is ");
//        text.append(order.getId());
//        
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        mailMessage.setTo(account.getEmail());
//        mailMessage.setFrom(this.mailFrom);
//        mailMessage.setSubject(this.subject);
//        mailMessage.setText(text.toString());
//        try {
//            this.mailSender.send(mailMessage);
//        } catch (MailException ex) {
//            // just log it and go on
//            logger.warn("An exception occured when trying to send email", ex);
//        }
//    }
//
//    public void afterPropertiesSet() throws Exception {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//    
//}
